package day13.demo2.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther:小陈的Java
 */
public class WindowTest {
    public static void main(String[] args) {
        Runnable runnable = new Window();
        new Thread(runnable,"窗口1").start();
        new Thread(runnable,"窗口2").start();
        new Thread(runnable,"窗口3").start();
    }
}
class Window implements Runnable{
    private int ticket = 100;
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true){
            lock.lock();
            try {
                if(ticket>0){
                        System.out.println(Thread.currentThread().getName()+":卖票，卖第"+ticket+"票");
                        Thread.sleep(10);
                        ticket--;
                }else {
                   break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}